/*
 * Copyright (c) 2025 CAX Conversion Project
 */
package com.comac.ins.isfd.service.impl.section;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.core.utils.MapstructUtils;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.encrypt.utils.EncryptUtils;
import com.comac.ins.isfd.constant.enums.IsfdSectionTypeEnum;
import com.comac.ins.isfd.domain.IsfdSection;
import com.comac.ins.isfd.domain.IsfdSectionResult;
import com.comac.ins.isfd.domain.bo.IsfdSectionBo;
import com.comac.ins.isfd.domain.vo.IsfdLayupEquivalentMaterialVo;
import com.comac.ins.isfd.util.SectionCalculateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 型材库 复材框中缘 实现类
 *
 * @author hxloongs
 * @date 2024-7-30
 */
@Service
public class IsfdSectionFgmServiceImpl extends IsfdAbstractSectionService {
    @Override
    public String selectByType() {
        return IsfdSectionTypeEnum.FGM.getValue();
    }

    @Override
    public double[] calculate(IsfdSectionBo isfdSectionBo) {
        String tLayerCode = isfdSectionBo.getTLayerCode();

        IsfdLayupEquivalentMaterialVo isfdLayupEquivalentMaterialVo = iIsfdLayupEquivalentMaterialService.queryByLayCode(tLayerCode);
        // 查询铺层库的代码，填充T E G 和 rho 、thm
        double[][] layerParametersFg = {{isfdLayupEquivalentMaterialVo.getT(), isfdLayupEquivalentMaterialVo.getExx(), isfdLayupEquivalentMaterialVo.getGxy()}}; // T, E, G
        double[] shapeParametersFg = {isfdSectionBo.getW()}; // w
        double[] sectionPropertiesFg = new double[8]; // E, G, XC, A, I1, I2
        SectionCalculateUtils.calculateFGProfile(layerParametersFg, shapeParametersFg, sectionPropertiesFg);
        sectionPropertiesFg[6] = isfdLayupEquivalentMaterialVo.getRho(); // rho
        sectionPropertiesFg[7] = isfdLayupEquivalentMaterialVo.getX(); // thm
        return sectionPropertiesFg;
    }

    @Transactional
    @Override
    public Boolean save(IsfdSectionBo bo) {
        // 1. 参数校验
        checkParams(bo);

        // 2. 获取hash字段
        String hashUniqueKey = EncryptUtils.hashFields(bo.getType(), bo.getSerialNumber(), bo.getAttributeNumber(), bo.getMat1Id(),
            bo.getW(), bo.getTLayerCode());

        // 3. 幂等校验
        Boolean isUpdate = super.checkUnique(bo, hashUniqueKey);

        // 4. 计算
        double[] sectionPropertiesFg = this.calculate(bo);

        // 5. 落表
        IsfdSection isfdSection = MapstructUtils.convert(bo, IsfdSection.class);
        isfdSection.setHashUniqueKey(hashUniqueKey);
        isfdSection.setRho(sectionPropertiesFg[6]);
        isfdSection.setThm(sectionPropertiesFg[7]);
        // 判断是新增业务还是修改业务，处理落表的方式不同
        if (isUpdate) {
            isfdSectionMapper.updateById(isfdSection);
            IsfdSectionResult isfdSectionResult = isfdSectionResultMapper.selectOne(new LambdaQueryWrapper<IsfdSectionResult>()
                .eq(IsfdSectionResult::getSecId, isfdSection.getId()));
            if (isfdSectionResult == null) {
                throw new BaseException("修改时，发现对应结果记录不存在，请确认后重试！");
            }
            isfdSectionResult.setE(sectionPropertiesFg[0]);
            isfdSectionResult.setG(sectionPropertiesFg[1]);
            isfdSectionResult.setXc(sectionPropertiesFg[2]);
            isfdSectionResult.setA(sectionPropertiesFg[3]);
            isfdSectionResult.setI1(sectionPropertiesFg[4]);
            isfdSectionResult.setI2(sectionPropertiesFg[5]);
            isfdSectionResultMapper.updateById(isfdSectionResult);
        } else {
            isfdSectionMapper.insert(isfdSection);
            IsfdSectionResult isfdSectionResult = new IsfdSectionResult();
            isfdSectionResult.setSecId(isfdSection.getId());
            isfdSectionResult.setE(sectionPropertiesFg[0]);
            isfdSectionResult.setG(sectionPropertiesFg[1]);
            isfdSectionResult.setXc(sectionPropertiesFg[2]);
            isfdSectionResult.setA(sectionPropertiesFg[3]);
            isfdSectionResult.setI1(sectionPropertiesFg[4]);
            isfdSectionResult.setI2(sectionPropertiesFg[5]);
            isfdSectionResultMapper.insert(isfdSectionResult);
        }
        return Boolean.TRUE;
    }


    private void checkParams(IsfdSectionBo isfdSectionBo) {
        if (StringUtils.isBlank(isfdSectionBo.getSerialNumber()) || !isfdSectionBo.getSerialNumber().startsWith("FG")) {
            throw new BaseException("编号不能为空或者格式不正确（需要FG开头），请确认后重试！");
        }
        if (StringUtils.isBlank(isfdSectionBo.getAttributeNumber())) {
            throw new BaseException("属性号不能为空，请确认后重试！");
        }
        if (StringUtils.isBlank(isfdSectionBo.getMat1Id())) {
            throw new BaseException("材料编号不能为空，请确认后重试！");
        }
        if (isfdSectionBo.getW() == null) {
            throw new BaseException("w不能为空，请确认后重试！");
        }
        if (StringUtils.isBlank(isfdSectionBo.getTLayerCode())) {
            throw new BaseException("t铺层代号不能为空，请确认后重试！");
        }
    }
}
